package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/6/26
 * @desc 演示做出响应
 */
@WebServlet("/resp")
public class Demo2Servlet extends HttpServlet {

    /**
     * 通过方法参数 HttpServletResponse resp完成响应
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求前设置编码格式
        req.setCharacterEncoding("UTF-8");


        // 接收请求
        String id = req.getParameter("id");
        System.out.println("id = " + id);
        // 做出响应(其他的不需要设置)
        // 重点是响应到浏览器的正文内容
        // 是通过输出流,写出到浏览器


        resp.setContentType("text/html;charset=utf-8");
        // 获得字符输出流
        PrintWriter writer = resp.getWriter( );
        writer.write("<html>");
        writer.write("<head>");
        writer.write("<title>这是响应</title>");
        writer.write("</head>");
        writer.write("<body>");
        writer.write("<h1>这是后端servlet响应</h1>");
        writer.write("<p style='color:red'> 接收到id = "+id+" </p>");
        writer.write("</body>");
        writer.write("</html>");

    }
}
